using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour
{
    public float score = 0;
    public GUIText hp;
    public GUIText sc;
    public float health = 100;
    public float maxHealth = 100;
    public float currentSpeed = 0;
    public float Speed = 100;
    public Shield shield;
    private CharacterController controller;
    private Shoot shootScript;

    public float slowPercentage = 0;
    public float slowTimespan = 0;
    public float stunTimespan = 0;

    public float poisonAmount = 0;
    public float poisonTimespan = 0;
    public float poisonDelay = 0;
    private float poisonedLast = 0;


    GameObject LifeStealParticles;
    GameObject ShieldParticles;
    // Use this for initialization
    void Start()
    {

        LifeStealParticles = GameObject.Find("LifeStealParticles");
        ShieldParticles = GameObject.Find("ShieldParticles");
        controller = (CharacterController)GetComponent("CharacterController");
        shootScript = (Shoot)GetComponent("Shoot");
        hp = GameObject.Find("hp").guiText;
        sc = GameObject.Find("sc").guiText;
       // Screen.showCursor = false;
        //Screen.lockCursor = true;
        //Screen.fullScreen = true;
    }

    private void slow(float percentage, float timespan)
    {
        slowPercentage = percentage;
        slowTimespan = timespan; 
    }
    private float getMovementSpeed()
    {
        if (stunTimespan > 0)
        {
            return 0;
        }
        if (slowTimespan > 0)
        {
            return Speed - (Speed / 100 * slowPercentage);
        }
        return Speed;
    }
    private void decreaseEffectCounters()
    {
        slowTimespan -= Time.deltaTime;
        if (slowTimespan < 0)
        {
            slowTimespan = 0;
        }
        stunTimespan -= Time.deltaTime;
        if (stunTimespan < 0)
        {
            stunTimespan = 0;
        }
        poisonTimespan -= Time.deltaTime;

        float r = poisonTimespan % poisonDelay;
        if (r > poisonedLast)
        {
            ApplyHealth(-poisonAmount);
        }
        poisonedLast = r;
        if (poisonTimespan < 0)
        {
            poisonTimespan = 0;
        }

    }

    void FixedUpdate()
    {
        if (!networkView.isMine)
        {
            return;
        }
        // Move forward / backward
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 sideward = transform.TransformDirection(Vector3.right);
        Vector3 upward = transform.TransformDirection(Vector3.up);
        float currentSpeed = getMovementSpeed() * Input.GetAxis("Vertical");
        float currentSidewardSpeed = getMovementSpeed() * Input.GetAxis("Horizontal");
        float currentUpwardSpeed = getMovementSpeed() * Input.GetAxis("Z");
        print(currentSpeed+" "+currentSidewardSpeed+" "+currentUpwardSpeed);
        controller.Move(forward * currentSpeed * Time.deltaTime);

        controller.Move(sideward * currentSidewardSpeed * Time.deltaTime);
        controller.Move(upward * currentUpwardSpeed * Time.deltaTime);


        sc.text = score.ToString();
        hp.text = health.ToString();
        decreaseEffectCounters();
        if (LifeStealParticles.particleEmitter.emit)
        {
            LifeStealParticles.particleEmitter.emit = false;
        }
        if (ShieldParticles.particleEmitter.emit)
        {
            ShieldParticles.particleEmitter.emit = false;
        }
    }
    public float ApplyDamage(float amount, Weapon weapon)
    {
        print("ApplyDamage" + amount);
        float dmg = shield.decreaseDamage(amount, weapon);
        print("decreaseDamage" + dmg);
        ApplyHealth(-dmg);
        if (weapon.liveStealPercentage > 0)
        {
            dmg = amount / 100 * weapon.liveStealPercentage;
        }
        else
        {
            dmg = 0;
        }


        stunTimespan = weapon.stunTimespan;
        slowPercentage =  weapon.slowPercentage;
        slowTimespan = weapon.slowTimespan;
        poisonAmount = weapon.poisonAmount;
        poisonDelay = weapon.poisonDelay;
        poisonTimespan = weapon.poisonTimespan;
        if (stunTimespan > 0)
        {
            shootScript.disable(stunTimespan);
        }
        return dmg;
    }
    public void ApplyHealth(float amount)
    {
        health += amount;
        if (amount > 0)
        {
            emitLifestealParticles();
        }
        else if(amount<0)
        {
            emitShieldParticles();
        }
        if (health > maxHealth)
        {
            health = maxHealth;
        }
        if (health < 0)
        {
            health = 0;
            // die();
        }
    }
    public void emitLifestealParticles()
    {
        LifeStealParticles.particleEmitter.emit = true;
    }
    public void emitShieldParticles()
    {
        LifeStealParticles.particleEmitter.emit = true;
    }
     
}
  